<?php

	class PasteEmbeddedCodeCommand implements ICommand
	{
		public function execute($event)
		{
			$name = $event->data['name'];
			$ref = $event->data['ref'];
			$folderid = $event->data['folder'];
			
			$folders = EbModel::getModel('Media','MediaFolder')->selectById($folderid);
			
			if (count($folders) == 0)
			{
				throw new Exception('The destination folder is not found!');
			}
			
			$extension_model 	= EbModel::getModel('Media','MediaExtension');			
			$extensions 		= $extension_model->selectByName('youtube');
			
			if (count($extensions) == 1)
			{
				$code 				= $event->data['code'];
				
				$media 				= (object) array('name' => $name, 'fd_id' => $folderid);
				$media_item_model 	= EbModel::getModel('Media','MediaItem');
				$media_id 			= EbModel::getModel('Media','Media')->insert($media);

				$mediaitem = (object) array('name' => $name,'pr_id' => 1,'mainyn' => 1, 'md_id' => $media_id,'ex_id' => $extensions[0]->ex_id);
						
				$media_item_model->insert($mediaitem);				
			
				Media_YoutubeProvider::createMediaItem($mediaitem,$code);
				
				echo $code;	
			}
		}
	}